class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        d = {ti:-1 for ti in tuple(s)}
        dc = d.copy()
        start = 0
        length = 0
        for i, c in enumerate(s):
            # print(dc)
            if dc[c] >= start:
                new_length = i - start
                start = dc[c] + 1
                if new_length >= length:
                    length = new_length
            dc[c] = i
        new_length = len(s) - start
        if new_length >= length:
            length = new_length
        return length


if __name__ == '__main__':
    s = Solution()
    print(s.lengthOfLongestSubstring('bbb'))
    print(s.lengthOfLongestSubstring('abcabcbb'))
    print(s.lengthOfLongestSubstring('pwwkew'))
    ## test case
    print(s.lengthOfLongestSubstring('a'))
    print(s.lengthOfLongestSubstring('dvdf'))
    print(s.lengthOfLongestSubstring('abba'))
